<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>avg 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-metrics-avg-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-avg-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-metrics-avg-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-metrics-avg-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-metrics-avg-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-metrics.html">度量聚合</a></span>
»
<span class="breadcrumb-node">avg 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-metrics.html">« 度量聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-weight-avg-aggregation.html">weighted_avg 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-metrics-avg-aggregation"></a>平均(avg)聚合
</h2>
</div></div></div>
<p>
<code class="literal">single-value(单值)</code>度量聚合，计算从聚合文档中提取的数值的平均值。

这些值可以从文档中指定的 numeric 字段中提取，也可以由给定的脚本生成。
</p>
<p>
假设数据由代表学生考试成绩(0到100之间)的文档组成，我们可以用以下公式计算他们的平均分数：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_search?size=0
{
    "aggs" : {
        "avg_grade" : { "avg" : { "field" : "grade" } }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/320.console"></div>
<p>
上面这个聚合计算所有文档的平均分数。

聚合类型为 <code class="literal">avg</code>，<code class="literal">field</code> 设置定义了计算平均值所依据的文档的 numeric 字段。

上面的查询请求将返回以下内容：
</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "avg_grade": {
            "value": 75.0
        }
    }
}</pre>
</div>
<p>
聚合的名称(上例中用的是<code class="word">avg_grade</code>)也用作key，通过该 key 可以从返回的响应中检索聚合结果。
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_script"></a>脚本(script)
</h3>
</div></div></div>
<p>基于脚本计算平均分数：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_search?size=0
{
    "aggs" : {
        "avg_grade" : {
            "avg" : {
                "script" : {
                    "source" : "doc.grade.value"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/321.console"></div>
<p>
这将把 <code class="literal">script</code> 参数解释为一个<code class="literal">inline</code>(内联)脚本，使用<code class="literal">painless</code>(无痛) 脚本语言，没有脚本参数。

要使用已存储的脚本，请使用以下语法：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_search?size=0
{
    "aggs" : {
        "avg_grade" : {
            "avg" : {
                "script" : {
                    "id": "my_script",
                    "params": {
                        "field": "grade"
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/322.console"></div>
<div class="section">
<div class="titlepage"><div><div>
<h4 class="title">
<a id="_value_script"></a>值脚本(value script)
</h4>
</div></div></div>
<p>
事实证明，这次考试远远超出了学生的水平，需要进行分数修正。

我们可以使用值脚本来获得新的平均值：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_search?size=0
{
    "aggs" : {
        "avg_corrected_grade" : {
            "avg" : {
                "field" : "grade",
                "script" : {
                    "lang": "painless",
                    "source": "_value * params.correction",
                    "params" : {
                        "correction" : 1.2
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/323.console"></div>
</div>

</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_missing_value"></a>缺失的值(missing value)
</h3>
</div></div></div>
<p>
参数 <code class="literal">missing</code> 定义应该如何处理有缺失的值的文档。

默认情况下，它们将被忽略，但也可以将它们视为有(一个默认)值。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /exams/_search?size=0
{
    "aggs" : {
        "grade_avg" : {
            "avg" : {
                "field" : "grade",
                "missing": 10 <a id="CO179-1"></a><i class="conum" data-value="1"></i>
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/324.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO179-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
<code class="word">grade</code>字段中没有值的文档将与值为<code class="literal">10</code>的文档属于同一个桶。
</p>
</td>
</tr>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-metrics.html">« 度量聚合</a>
</span>
<span class="next">
<a href="search-aggregations-metrics-weight-avg-aggregation.html">weighted_avg 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>